//
//  HSDSpidermanNetworkManager.m
//  GoldenCard
//
//  Created by 正合适 on 2018/3/22.
//  Copyright © 2018年 合时代. All rights reserved.
//

#import "HSDSpidermanNetworkManager.h"

static NSString * requestErrorSerializer(NSError * error) {
    if (error.code == -1001) {
        return @"网络超时";
    }
    if (error.code == -1009) {
        return @"网络异常";
    }
    return [NSString stringWithFormat:@"%@(%ld)", @"服务器异常", (long)error.code];
}

static NSString *jsonStr(id val) {
    return [[NSString alloc] initWithData:val encoding:NSUTF8StringEncoding];
}

@interface HSDSpidermanNetworkManager ()
/**af网络管理*/
@property (nonatomic, strong) AFHTTPSessionManager *manager;
/**网络装填管理类*/
@property (nonatomic, strong) AFNetworkReachabilityManager *netManager;
@end

@implementation HSDSpidermanNetworkManager

#pragma mark - 单例
+ (instancetype)instance {
    static HSDSpidermanNetworkManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HSDSpidermanNetworkManager alloc] init];
    });
    return instance;
}

//#pragma mark - 初始化
- (instancetype)init {
    self = [super init];
    if (self) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//        [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    }
    return self;
}

- (void)requestWithType:(RequestType)type url:(NSString *)url params:(id)params success:(SuccessBlock)successBlock error:(ErorrBlock)errorBlock {
    if (type == RequestType_Get) {
        [_manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successBlock) successBlock(jsonStr(responseObject));
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (errorBlock) errorBlock(requestErrorSerializer(error));
        }];
    } else if (type == RequestType_Post) {
        [_manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successBlock) successBlock(jsonStr(responseObject));
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (errorBlock) errorBlock(requestErrorSerializer(error));
        }];
    }
}

- (void)requestWithType:(RequestType)type url:(NSString *)url params:(id)params header:(NSDictionary *)headerDic success:(SuccessBlock)successBlock error:(ErorrBlock)errorBlock {
    if (headerDic.allKeys.count > 0) {
        for (NSInteger i = 0; i < headerDic.allKeys.count; i ++) {
            NSString *key = headerDic.allKeys[i];
            [_manager.requestSerializer setValue:headerDic[key] forHTTPHeaderField:key];
        }
    }
    if (type == RequestType_Get) {
        [_manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successBlock) successBlock(jsonStr(responseObject));
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (errorBlock) errorBlock(requestErrorSerializer(error));
        }];
    } else if (type == RequestType_Post) {
        [_manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successBlock) successBlock(jsonStr(responseObject));
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (errorBlock) errorBlock(requestErrorSerializer(error));
        }];
    }
}


@end
